<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class PayController extends Controller
{
    
    public function index(Request $request)
    {
    	$uid = $request->input('uid');
    	$db = DB::table("distribution")->where("uid",$uid)->update(['state' => '2']);
    	$carid = session()->get('carid');
        foreach ($carid as $v) {
            $d = DB::table('car')->where('carid',$v)->get();
            $goodsname = $d[0]->goodsname;
            $goodsprice = $d[0]->goodsprice;
            $goodsnum = $d[0]->goodsnum;
            $prices = $d[0]->prices;
            $good = DB::table('goods')->where('goodsname',$goodsname)->get();
            $stock = $good[0]->stock;
            $varstock = $good[0]->varstock;
            $stocknum = $varstock-$goodsnum;
            $allnum = $stock-$stocknum;
            $data = array('goodsname'=>$goodsname,'goodsprice'=>$goodsprice,'stock'=>$stock,'varstock'=>$varstock,'allnum'=>$allnum,'goodsnum'=>$goodsnum,'prices'=>$prices,'stocknum'=>$stocknum);
            $good = array('varstock'=>$stocknum);
            DB::table('goods')->where('goodsname',$goodsname)->update($good);
            DB::table('control')->insert($data);
            DB::table('car')->where('carid',$v)->delete();
        }
    	
    	echo json_encode($uid);
    	 
    }
}
